<template>
	<view class="container">
		<u-navbar   title="个人图片"	:fixed="true"	:border-bottom="false" > </u-navbar>
		
		<view class="pre-box">
			<view @longpress="onlang" class="pre-item" v-for="(item, index) in images" :key="index">
				<image @click.stop="previewImage(item)" class="pre-item-image" :src="item" ></image>
				<view v-if="rme" class="remove" @click="remmove(index)">
					<image src="../../static/icon/remove.png" mode="widthFix"></image>
				</view>
			</view>		
			<u-upload v-if="!other" :custom-btn="true" :show-upload-list="false"  ref="uUpload" :action="action"
					  @on-change='upload' :max-size="5 * 1024 * 1024" max-count="6" 
					  :form-data='uploadFormData' >
					  <view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
					  		<image src="../../static/icon/upload.png" mode="widthFix"></image>
							<view>上传图片</view>
					  </view>
			 </u-upload>
		</view>
			
		
		<view v-if="!other" class="btn" @click="submit">保存</view>
	</view>
</template>

<script>
	import {editUserInfo,getUserInfo} from '@/api/index.js'
	export default {
		data() {
			return {
				action:'',
				uploadFormData: new Object(),
				rme:true,
				images:[],
				userId:'',
				userinfo:{},
				other:true
			}
		},
		onLoad(e) {
			this.userId=Number(e.id)
		},
		onShow() {
			let user=this.ws.getUserInfo()
			if(user.user_id==this.userId){
				this.other=false
			}
			this.init()
			this.action=this.ws.upload;
		},
		methods: {
			init(){
				let that=this
				getUserInfo({'user_id':that.userId}).then(res=>{
					if(res.code==1){
						that.userinfo=res.data.info
						that.images=that.userinfo.images
						if(that.images.length>0) that.rme=false
					}else{
						uni.showToast({
							title:res.msg,
							icon:'none'
						})
					}
				})
			},
			submit() {
					let that=this,src=''
					that.images.forEach((item,index)=>{
						if(index==0) {
							src=item
						}else{
							src=src+','+item
						}
					})
					that.userinfo.images=src
					editUserInfo(that.userinfo).then(res=>{
						let msg='失败'
						if(res.code==1){
							that.rme=false
							msg='成功'
						}
						uni.showToast({
							title:msg
						})
					})
			},	
			// 上传成功回调
			upload(res, index, lists, name){
				res = JSON.parse(res.data);
				this.images.push(res.data.fullurl)
			},
			// 放大预览
			previewImage(url){
				uni.previewImage({
					urls: [url],
				})
			},
			// 删除
			remmove(index){
				this.images.splice(index,1)
			},
			// 长按显示
			onlang(){
				if(!this.other)this.rme=true
				
			}
		}
	}
</script>

<style lang="less">
	.container{
		background-color: #FFF;
		height: 100vh;
		padding: 15px;
		
		.btn{
			position: fixed;
			bottom: 40px;
			border-radius: unset;
			left: 5vw;
		}
		u-upload{
			width: 33.3%;
			display: flex;
			justify-content: center;
			align-items: center;
			margin-bottom: 20px;
		}
		.slot-btn{
			border-radius: 8.5px;
			border: 1rpx dashed  #b4b4b4;
			box-sizing: border-box;
			display: flex;
			justify-content: center;
			flex-flow: column;
			align-items: center;
			height: 100px;width: 100px;
			image{
				width: 35px;
				margin-bottom: 10px;
			}
		}
		
		.pre-box {
			 display: flex;
			 align-items: center;
			 justify-content: flex-start;
			 flex-wrap: wrap;
			.pre-item{
				width: 33.3%;
				margin-bottom: 20px;
				display: flex;
				align-items: center;
				justify-content: center;
				position: relative;
				.remove{
					position: absolute;
					z-index: 99;
					left:calc((100% - 100px)/2 + 83px);
					top: 0;
					width: 20px;height: 20px;
					image{
						width: 17px;
					}
				}
			}
			.pre-item-image {
				width: 100px;
				height: 100px;
				border-radius: 8.5px;
			}
		}
	}
	
</style>